﻿using System;
using Runtime.Services.Battle;
using UnityEngine;

namespace Runtime.Services.BattleAttack {

    internal sealed partial class DFR_BattleAttackService {

        private sealed class MapPositionProvider : IDFR_BattleMapPositionProvider {

            private IDFR_BattleMapPositionProvider m_Target;

            public MapPositionProvider(IDFR_BattleMapPositionProvider target) {
                m_Target = target;
            }

            public bool IsEnabled => true;

            private DFR_BattleMapPosition m_LastMapPosition;
            public DFR_BattleMapPosition LastMapPosition => m_LastMapPosition + m_RenderOffset;

            public DFR_BattleMapPosition MapPosition {
                get {
                    if (m_Target != null) {
                        if (m_Target.IsEnabled) {
                            m_LastMapPosition = m_Target.MapPosition + m_RenderOffset;
                        } else {
                            m_Target = null; // TODO: 临时性的置null，需要从外部正确释放子弹锁定的目标
                        }
                    }
                    return m_LastMapPosition;
                }
            }

            public F64 MoveSpeed => m_Target != null ? m_Target.MoveSpeed : F64.Zero;

            private DFR_BattleMapPosition m_tempSupposeMove;
            public DFR_BattleMapPosition SupposeMove(F64 seconds) {
                if (m_Target != null && m_Target.IsEnabled) {
                    m_tempSupposeMove = m_Target.SupposeMove(seconds) + m_RenderOffset;

                }
                return m_tempSupposeMove;
            }

            private Vector2F64 m_RenderOffset;
            public Vector2F64 RenderOffset {
                get {
                    if (m_Target != null && m_Target.IsEnabled) {
                        m_RenderOffset = m_Target.RenderOffset;
                    }
                    return m_RenderOffset;
                }
            }

            public void Dispose() {
                m_Target = null;
            }
        }
    }
}
